package others;

public class test845 {
    /**
     * 数组中的最长山脉
     * 把符合下列属性的数组 arr 称为 山脉数组 ：
     * <p>
     * arr.length >= 3
     * 存在下标 i（0 < i < arr.length - 1），满足
     * arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
     * arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
     * 给出一个整数数组 arr，返回最长山脉子数组的长度。如果不存在山脉子数组，返回 0 。
     * 输入：arr = [2,1,4,7,3,2,5]
     * 输出：5
     * 解释：最长的山脉子数组是 [1,4,7,3,2]，长度为 5。
     */

    public static int longestMountain(int[] arr) {

        int res = 0;
        int res1 = 0;
        int res2 = 0;
        if (arr.length < 3) {
            return 0;
        }
        for (int i = 2; i < arr.length; i++) {
            int left = i-1, right = i-1;
            if (arr[i-1] > arr[i - 2] && arr[i-1] > arr[i]) {
                while (left > 0 && arr[left] > arr[left - 1]) {
                    left--;
                    res1 = left;
                }
                while (right < arr.length-1 && arr[right] > arr[right+1]) {
                    right++;
                    res = right;
                }
                res2 =res2 > res - res1 + 1 ?res2:res - res1 + 1;
            }
        }
        return res2;
    }

    public static void main(String[] args) {
//        int[] nums = {2,1,4,7,3,2,5};
//        int[] nums = {0,1,2,3,4,5,4,3,2,1,0};
//        int[] nums = {0,1,2,3,4,5,6,7,8,9};
//        int[] nums = {0,1,0};
        int[] nums = {875,884,239,731,723,685};
//        int[] nums = {40,51,29,19,50,25};
        System.out.println(longestMountain(nums));
    }
}
